package com.tencent.wxcloudrun.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencent.wxcloudrun.domain.TStoreProd;
import com.tencent.wxcloudrun.enums.StoreProdStatEnum;
import com.tencent.wxcloudrun.exception.BusinessException;
import com.tencent.wxcloudrun.mapper.TStoreProdMapper;
import com.tencent.wxcloudrun.service.TStoreProdService;
import com.tencent.wxcloudrun.vo.request.store.StoreProdAddReqVo;
import com.tencent.wxcloudrun.vo.request.store.StoreProdListReqVo;
import com.tencent.wxcloudrun.vo.request.store.StoreProdUpdateReqVo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @author ziyin.zhao
 * @description 针对表【t_store_prod(商家商品表)】的数据库操作Service实现
 * @createDate 2025-04-22 16:43:56
 */
@Slf4j
@Service
public class TStoreProdServiceImpl extends ServiceImpl<TStoreProdMapper, TStoreProd> implements TStoreProdService {

    @Override
    public Long addProd(StoreProdAddReqVo reqVo) {
        TStoreProd storeProd = new TStoreProd();
        storeProd.setProdTypeId(reqVo.getProdTypeId());
        storeProd.setName(reqVo.getName());
        storeProd.setContent(reqVo.getContent());
        storeProd.setImgUrlList(reqVo.getImgUrlList());
        storeProd.setStatus(StoreProdStatEnum.LAUNCH.getValue());
        storeProd.setPrice(reqVo.getPrice());
        storeProd.setTotalStock(reqVo.getTotalStock());
        storeProd.setMultipleFlag(reqVo.getMultipleFlag());
        storeProd.setLimitFlag(reqVo.getLimitFlag());
        storeProd.setLimitMinVol(reqVo.getLimitMinVol());
        storeProd.setLimitMaxVol(reqVo.getLimitMaxVol());
        storeProd.setCreatedTime(LocalDateTime.now());
        storeProd.setUpdatedTime(LocalDateTime.now());
        storeProd.setCreatedBy(String.valueOf(reqVo.getUserId()));
        storeProd.setUpdatedBy(String.valueOf(reqVo.getUserId()));
        this.save(storeProd);
        return storeProd.getId();
    }

    @Override
    public void updateProd(StoreProdUpdateReqVo reqVo) {
        TStoreProd storeProd = this.getById(reqVo.getProdId());
        if (Objects.isNull(storeProd)) {
            log.error("更新商品-商品不存在-商品ID={}", reqVo.getProdId());
            throw new BusinessException("商品不存在");
        }
        storeProd.setProdTypeId(reqVo.getProdTypeId());
        storeProd.setName(reqVo.getName());
        storeProd.setContent(reqVo.getContent());
        storeProd.setImgUrlList(reqVo.getImgUrlList());
        storeProd.setPrice(reqVo.getPrice());
        storeProd.setTotalStock(reqVo.getTotalStock());
        storeProd.setMultipleFlag(reqVo.getMultipleFlag());
        storeProd.setLimitFlag(reqVo.getLimitFlag());
        storeProd.setLimitMinVol(reqVo.getLimitMinVol());
        storeProd.setLimitMaxVol(reqVo.getLimitMaxVol());
        storeProd.setUpdatedTime(LocalDateTime.now());
        storeProd.setUpdatedBy(String.valueOf(reqVo.getUserId()));
        this.updateById(storeProd);
    }

    @Override
    public void putOnProd(Long userId, Long prodId) {
        LambdaUpdateWrapper<TStoreProd> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TStoreProd::getStatus, StoreProdStatEnum.LAUNCH.getValue());
        wrapper.set(TStoreProd::getUpdatedTime, LocalDateTime.now());
        wrapper.set(TStoreProd::getUpdatedBy, String.valueOf(userId));
        wrapper.eq(TStoreProd::getId, prodId);
        this.update(wrapper);
    }

    @Override
    public void takeOffProd(Long userId, Long prodId) {
        LambdaUpdateWrapper<TStoreProd> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TStoreProd::getStatus, StoreProdStatEnum.TAKE_DOWN.getValue());
        wrapper.set(TStoreProd::getUpdatedTime, LocalDateTime.now());
        wrapper.set(TStoreProd::getUpdatedBy, String.valueOf(userId));
        wrapper.eq(TStoreProd::getId, prodId);
        this.update(wrapper);
    }

    @Override
    public void deleteProd(Long userId, Long prodId) {
        LambdaUpdateWrapper<TStoreProd> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TStoreProd::getDeleteFlag, 1);
        wrapper.set(TStoreProd::getEnableFlag, 0);
        wrapper.set(TStoreProd::getUpdatedTime, LocalDateTime.now());
        wrapper.set(TStoreProd::getUpdatedBy, String.valueOf(userId));
        wrapper.eq(TStoreProd::getId, prodId);
        this.update(wrapper);
    }

    @Override
    public List<TStoreProd> listProd(StoreProdListReqVo reqVo) {
        LambdaQueryWrapper<TStoreProd> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(reqVo.getKeyWord()), TStoreProd::getName, reqVo.getKeyWord());
        queryWrapper.eq(reqVo.getStatus() != null, TStoreProd::getStatus, reqVo.getStatus());
        queryWrapper.eq(reqVo.getProdTypeId() != null, TStoreProd::getProdTypeId, reqVo.getProdTypeId());
        queryWrapper.eq(TStoreProd::getEnableFlag, 1);
        return this.list(queryWrapper);
    }
}
